package com.ithaipeng.redanttakeaway.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ithaipeng.redanttakeaway.pojo.AddressBook;
import com.ithaipeng.redanttakeaway.service.IAddressBookService;
import com.ithaipeng.redanttakeaway.tools.BaseContext;
import com.ithaipeng.redanttakeaway.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private IAddressBookService addressBookService;

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        boolean isSuccess = addressBookService.save(addressBook);
        if (isSuccess){
            return  Result.success("新增地址成功!");
        }else {
            return Result.error("新增地址失败");
        }
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public  Result<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        addressBookLambdaUpdateWrapper.eq(BaseContext.getCurrentId()!=null,AddressBook::getUserId,BaseContext.getCurrentId());
        addressBookLambdaUpdateWrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(addressBookLambdaUpdateWrapper);
        addressBook.setIsDefault(1);
        boolean isSuccess = addressBookService.updateById(addressBook);
        if (isSuccess){
            return Result.success(addressBook);
        }else {
            return Result.error("修改失败!");
        }
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> select(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook!=null){
            return Result.success(addressBook);
        }else {
            return Result.error("没有找到该地址!");
        }
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> selectByDefault(){
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper=new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        addressBookLambdaQueryWrapper.eq(BaseContext.getCurrentId()!=null,AddressBook::getUserId,BaseContext.getCurrentId());
        AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
        if (null == addressBook) {
            return Result.error("没有找到该对象");
        } else {
            return Result.success(addressBook);
        }
    }

    /**
     * 查询指定用户的全部地址
     * @param addressBook
     * @return
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper=new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(addressBook.getUserId()!=null,AddressBook::getUserId,addressBook.getUserId());
        addressBookLambdaQueryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> addressBookList = addressBookService.list(addressBookLambdaQueryWrapper);
        if (addressBookList.isEmpty()){
            return Result.error("该用户没有添加地址信息!");
        }else {
            return Result.success(addressBookList);
        }
    }

    /**
     * 修改地址
     */
    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook){
        boolean isSuccess = addressBookService.updateById(addressBook);
        if (isSuccess){
            return Result.success("修改成功!");
        }else {
            return Result.error("修改失败!");
        }
    }
    @DeleteMapping
    public Result<String> delete(Long ids){
        boolean isSuccess = addressBookService.removeById(ids);
        if (isSuccess){
            return Result.success("删除成功!");
        }else {
            return Result.error("删除失败!");
        }
    }
}
